package Action.Manager;

import Action.Sys.Setting;
import Bean.BookInfoEntity;
import Bean.Log.HistoricalData;
import Bean.Log.SystemLogEntity;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.hibernate.Session;
import tools.HibernateTools;

import java.util.List;

public class BookInfoManage extends ActionSupport {
    List<BookInfoEntity> bookInfoEntities;
    String key;
    int modify_id;
    String flag;
    BookInfoEntity bookInfoEntity;

    int name=0;
    Session session;
    //注意本类被客户端复用,不能进行二次登录校验
    public String show() throws Exception {
        session = HibernateTools.getSession();
        bookInfoEntities=session.createQuery("from BookInfoEntity ORDER BY id ").getResultList();
        if (bookInfoEntities.size()==0)
        {
            addActionError("没有图书信息");
            HibernateTools.commit(session);
            return INPUT;
        }
        for (BookInfoEntity infoEntity : bookInfoEntities) {
            infoEntity.build_image(false);
        }
//        SystemLogEntity.addLog(new AdministratorEntity(), this, "查看图书信息",SystemLogEntity.remark_book);
        clearActionErrors();
        HibernateTools.commit(session);
        return SUCCESS;
    }
    public String find() throws Exception {
        if (key==null||key.equals(""))
            return show();
         session= HibernateTools.getSession();
        int key_num=-1;
        try {
            key_num=Integer.valueOf(key);
        }catch (Exception e){
            key_num=-1;
        }
        bookInfoEntities=session.createQuery("from BookInfoEntity as book where book.book_name like ? or book.book_Author like ? or " +
                " book.book_Info like ? or cast(book.id as string ) like ? or book.book_tag1 like ?")
                .setParameter(0,"%"+key+"%")
                .setParameter(1, "%"+key+"%")
                .setParameter(2, "%"+key+"%")
                .setParameter(3, "%"+key+"%")
                .setParameter(4, "%"+key+"%")
                .getResultList();
        if (bookInfoEntities.size()==0)
        {
            addActionError("没有相关图书信息");
            HibernateTools.commit(session);
            return INPUT;
        }
//        SystemLogEntity.addLog(new AdministratorEntity(), this, "查看图书信息",SystemLogEntity.remark_book);
        clearActionErrors();
        HibernateTools.commit(session);
        return SUCCESS;
    }

    public String del() throws Exception {

        session=HibernateTools.getSession();
        bookInfoEntity=getBookInfo(modify_id);
        session.delete(bookInfoEntity);

        HistoricalData historicalData=session.get(HistoricalData.class, tools.TimeTools.formatTime(0));
        if (historicalData==null){
            historicalData=new HistoricalData();
            historicalData.setTimestamp(tools.TimeTools.formatTime(0));
        }
        historicalData.setNewBookNum(historicalData.getNewBookNum()-1);
        session.saveOrUpdate(historicalData);


        SystemLogEntity.addLog((Integer) ActionContext.getContext().getSession().get("adminid"), this, Setting.SystemLog_flag_DelBookData,bookInfoEntity.getBook_name());
        clearActionErrors();
        HibernateTools.commit(session);
        return SUCCESS;
    }

    private BookInfoEntity getBookInfo(int id) {
        return session.get(BookInfoEntity.class, id);
    }

    public List<BookInfoEntity> getBookInfoEntities() {
        return bookInfoEntities;
    }

    public void setBookInfoEntities(List<BookInfoEntity> bookInfoEntities) {
        this.bookInfoEntities = bookInfoEntities;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public int getModify_id() {
        return modify_id;
    }

    public void setModify_id(int modify_id) {
        this.modify_id = modify_id;
    }

    public String getFlag() {
        return flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    public BookInfoEntity getBookInfoEntity() {
        return bookInfoEntity;
    }

    public void setBookInfoEntity(BookInfoEntity bookInfoEntity) {
        this.bookInfoEntity = bookInfoEntity;
    }
}
